<template>
  <div class="knowledge-detail-page">
    <front-header></front-header>
    <div class="detail-card">
      <el-image v-if="detail.imageUrl" :src="getImageUrl(detail.imageUrl)" class="main-img" fit="cover" />
      <div class="detail-title">{{ detail.title }}</div>
      <div class="detail-summary">{{ detail.summary }}</div>
      <div class="detail-content" v-html="detail.content"></div>
      <div v-if="detail.extraImages && detail.extraImages.trim()" class="extra-images">
        <el-image v-for="img in detail.extraImages.split(',').map(i => i.trim()).filter(i => i)" :key="img" :src="getImageUrl(img)" class="extra-img" fit="cover" />
      </div>
    </div>
    <front-footer></front-footer>
  </div>
</template>
<script>
import { getKnowledgeList } from '@/api/AgricultureKnowledgeController'
import FrontHeader from '@/components/front/FrontHeader.vue'
import FrontFooter from '@/components/front/FrontFooter.vue'

/**
 * 农业知识详情页组件
 */
export default {
  name: 'FrontAgricultureKnowledgeDetail',
  components: {
    FrontHeader,
    FrontFooter
  },
  data() {
    return {
      detail: {}
    }
  },
  methods: {
    /**
     * 获取图片的完整 URL
     * @param {string} url - 图片的原始 URL
     * @returns {string} - 处理后的图片 URL
     */
    getImageUrl(url) {
      if (!url) return ''
      return url.startsWith('/api') ? url : `/api${url}`
    },
    /**
     * 加载知识详情
     */
    async loadDetail() {
      // 假设通过id获取详情，实际可根据你的API调整
      const id = this.$route.params.id
      const list = await getKnowledgeList()
      this.detail = list.find(item => String(item.id) === String(id)) || {}
    }
  },
  mounted() {
    this.loadDetail()
  }
}
</script>
<style scoped>
.knowledge-detail-page {
  padding: 32px 24px;
  background: #f9f9f9;
  min-height: 100vh;
  max-height: 100%;
  display: flex;
  flex-direction: column;
  color: #333333;
  max-height: 100%;
  overflow: auto;
}
.detail-card {
  flex: 1;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  padding: 32px 40px;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #eaeaea;
}
.main-img {
  width: 100%;
  height: 260px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 24px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border: 1px solid #eaeaea;
}
.detail-title {
  font-size: 28px;
  font-weight: 600;
  color: #333333;
  margin-bottom: 16px;
  letter-spacing: 0.5px;
}
.detail-summary {
  color: #666666;
  font-size: 16px;
  margin-bottom: 24px;
  line-height: 1.6;
  padding-left: 12px;
  border-left: 3px solid #4caf50;
}
.detail-content {
  font-size: 15px;
  color: #333333;
  margin-bottom: 24px;
  line-height: 1.8;
}
.detail-content :deep(h1),
.detail-content :deep(h2),
.detail-content :deep(h3),
.detail-content :deep(h4) {
  color: #333333;
  margin-top: 24px;
  margin-bottom: 16px;
}
.detail-content :deep(p) {
  margin-bottom: 16px;
}
.detail-content :deep(a) {
  color: #4caf50;
  text-decoration: none;
}
.detail-content :deep(a:hover) {
  text-decoration: underline;
}
.detail-content :deep(ul),
.detail-content :deep(ol) {
  padding-left: 20px;
  margin-bottom: 16px;
}
.detail-content :deep(li) {
  margin-bottom: 8px;
}
.detail-content :deep(img) {
  max-width: 100%;
  border-radius: 8px;
  margin: 16px 0;
}
.extra-images {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-top: 24px;
}
.extra-img {
  width: 180px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border: 1px solid #eaeaea;
  transition: transform 0.3s ease;
}
.extra-img:hover {
  transform: scale(1.05);
}

@media (max-width: 768px) {
  .detail-card {
    padding: 24px 20px;
  }
  .main-img {
    height: 200px;
  }
  .detail-title {
    font-size: 24px;
  }
  .extra-img {
    width: calc(50% - 8px);
    height: 100px;
  }
}
</style>